package com.dragon.logical.service.auth.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dragon.common.model.ResultCode;
import com.dragon.common.model.exception.CustomException;
import com.dragon.common.util.CovertUtil;
import com.dragon.logical.service.auth.IWorkflowService;
import com.dragon.repository.entity.Workflow;
import com.dragon.repository.entity.WorkflowJson;
import com.dragon.repository.mapper.WorkflowMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 工作流配置表 服务实现类
 * </p>
 *
 * @author sy
 * @since 2023-07-04
 */
@Service
public class WorkflowServiceImpl extends ServiceImpl<WorkflowMapper, Workflow> implements IWorkflowService {


    @Autowired
    WorkflowMapper mapper;

    @Override
    public Page<Workflow> selectPageList(Integer type, String name, Integer page, Integer limit) {
        Page<Workflow> workflowPage = new Page<>(page, limit);
        QueryWrapper<Workflow> workflowQueryWrapper = new QueryWrapper<>();
        workflowQueryWrapper.lambda()
                .eq(type != null, Workflow::getType, type).
                like(StrUtil.isNotBlank(name), Workflow::getName, name);
        Page<Workflow> pageList = mapper.selectPageList(workflowPage, workflowQueryWrapper);
        return pageList;
    }


    @Override
    public void addWorkflow(Workflow workflow) {
        //检测当前流程类型是否有默认工作流
        if (workflow.getDefaultOrNot() == 1) {
            //有，则更改之前的默认为不默认
            QueryWrapper<Workflow> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda().eq(Workflow::getDefaultOrNot, 1).eq(Workflow::getType, workflow.getType());
            Workflow workflow1 = mapper.selectOne(queryWrapper);
            if (workflow1 != null) {
                workflow1.setDefaultOrNot(0);
                workflow1.setUpdateTime(DateUtil.date());
                mapper.updateById(workflow1);
            }
        }
        workflow.setCreateTime(DateUtil.date());
        mapper.insert(workflow);
    }

    @Override
    public void updateWorkflow(Workflow workflow) {
        if (workflow.getState() != null) {
            if (workflow.getState() == 1) {
                //有，则更改之前的默认为不默认
                QueryWrapper<Workflow> queryWrapper = new QueryWrapper<>();
                queryWrapper.lambda().eq(Workflow::getDefaultOrNot, 1).eq(Workflow::getType, workflow.getType());
                Workflow workflow1 = mapper.selectOne(queryWrapper);
                if (workflow1 != null && workflow1.getId() != workflow.getId()) {
                    workflow1.setDefaultOrNot(0);
                    workflow1.setUpdateTime(DateUtil.date());
                    mapper.updateById(workflow1);
                }
            }
        }
        mapper.updateById(workflow);
    }

    @Override
    public void deleteWorkflow(String ids) {
        List<Integer> idList = CovertUtil.covertIntegerList(ids);
        QueryWrapper<Workflow> queryWrapper = new QueryWrapper<>();
        if (idList.size() != 0) {
            queryWrapper.lambda().in(Workflow::getId, idList);
        }
        mapper.delete(queryWrapper);
    }


    @Override
    public Workflow selectWorkflowById(Integer id) {
        Workflow workflow = mapper.selectById(id);
        if (workflow == null) {
            throw new CustomException(ResultCode.DATA_NOT_FOUND);
        }
        return workflow;
    }


    @Override
    public List<Workflow> selectListByType(Integer type) {
        QueryWrapper<Workflow> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(Workflow::getType, type).eq(Workflow::getState, 1);
        List<Workflow> list = mapper.selectList(queryWrapper);
        return list;
    }
}
